import {useState,useEffect} from "react";
import axios from 'axios';

// hooks的规则：
// 1、函数名必须以use开头，用小驼峰（use后面紧跟的字母必须大写）
// 2、普通函数里不能调用hooks
// 3、hooks只能在组件里或者其它hooks里使用（调用）

export function useProList() {
  const [count, setCount] = useState(1);
  const [limitNum, setLimitNum] = useState(10);
  const [proList, setProList] = useState([]);

  useEffect(() => {
    axios({
      url: "http://121.89.205.189:3000/api/pro/search",
      params: {
        count, limitNum
      }
    })
      .then(res => {
        if (res.data.code === "200") {
          setProList(res.data.data);
        }
      })
  }, [count, limitNum]);

  return {
    count, setCount,
    limitNum, setLimitNum,
    proList
  }
  
}


export function useProListAsync() {

  const [count, setCount] = useState(1);
  const [limitNum, setLimitNum] = useState(10);
  const [proList, setProList] = useState([]);

  useEffect(()=>{    
    async function getData(){
      let res = await axios({
        url: "http://121.89.205.189:3000/api/pro/search",
        params: {
          count, limitNum
        }
      })

      if (res.data.code === "200") {
        setProList(res.data.data);
      }
    } 

    getData();
    
  }, [count, limitNum]);

  return {
    count, setCount,
    limitNum, setLimitNum,
    proList
  }
  
}